package com.xysq.adapter;

/**
 * Created by Steward_Han on 2015/7/20.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.rockcent.model.EvaluateBO;
import com.xysq.XYApplication;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;

import java.text.SimpleDateFormat;
import java.util.List;

public class EvaluateAdapter extends BaseAdapter {

    private ViewHolder viewHolder;
    private Context context = null;
    private List<EvaluateBO> evaluateBOList = null;
    private ImageLoader imageLoader;

    public EvaluateAdapter(Context context, List<EvaluateBO> evaluateBOList, ImageLoader imageLoader) {
        this.context = context;
        this.evaluateBOList = evaluateBOList;
        this.imageLoader = imageLoader;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (null != evaluateBOList) {
            count = evaluateBOList.size();
        }
        return count;
    }

    @Override
    public EvaluateBO getItem(int position) {
        EvaluateBO item = null;
        if (null != evaluateBOList) {
            item = evaluateBOList.get(position);
        }
        return item;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder = new ViewHolder();
        if (null == convertView) {
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.item_evaluate, null);

            viewHolder.pic = (ImageView) convertView.findViewById(R.id.img_head);
            viewHolder.name = (TextView) convertView.findViewById(R.id.txt_customer_name);
            viewHolder.school = (TextView) convertView.findViewById(R.id.txt_school);
            viewHolder.date = (TextView) convertView.findViewById(R.id.txt_date);
            viewHolder.evaluate = (TextView) convertView.findViewById(R.id.txt_evaluate);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final EvaluateBO item = getItem(position);
        if (null != item) {
            viewHolder.pic.setTag(item.getHeadimgurl());
            if(TextUtils.isEmpty(item.getCommentUserName())){
                viewHolder.name.setText("匿名");
            }else {
                viewHolder.name.setText(item.getCommentUserName());
            }
            viewHolder.evaluate.setText(item.getContent());
            try {
                viewHolder.date.setText(timeChangeFormat(Long.valueOf(item.getCreateTime())));
            }catch (Exception e){
                viewHolder.date.setText(item.getCreateTime());
            }

            viewHolder.school.setText(item.getSchoolName());
            if (!TextUtils.isEmpty(item.getHeadimgurl()) && !item.getHeadimgurl().equals("null")) {
                if (imageLoader == null) {
                    imageLoader = XYApplication.getImageLoader();
                }
                imageLoader.loadImage(PropertiesUtil.getImageServer() + item.getHeadimgurl(), new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {
                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {
                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                        if (viewHolder.pic.getTag() != null && viewHolder.pic.getTag().toString().equals(item.getHeadimgurl())) {
                            Log.e("++++url", item.getHeadimgurl());
                            viewHolder.pic.setImageBitmap(bitmap);
                        }
                        notifyDataSetChanged();
                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {
                    }
                });
            }else {
                viewHolder.pic.setImageResource(R.drawable.def_img_head);
            }
        }
        return convertView;
    }

    private static class ViewHolder {
        ImageView pic;
        TextView name;
        TextView date;
        TextView school;
        TextView evaluate;
    }

    private String timeChangeFormat(Long data) {
        SimpleDateFormat format = new SimpleDateFormat("MM月d日 hh:mm");
        String time = format.format(data);
        return time;
    }
}